// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include <QFormLayout>
#include "RenderWidget.h"

RenderWidget::RenderWidget(QWidget *parent) :
    CollapsibleWidget(parent),
    m_renderGlowCheckBox(new QCheckBox()),
    m_sceneRenderTimeLabel(new QLabel()),
    m_glowTextureSizeCombo(new QComboBox()),
    m_renderTextureSizeCombo(new QComboBox()),
    m_blurSamplesCombo(new QComboBox())
{
    setTitle("Render Options");

    m_renderGlowCheckBox->setChecked(true);

    m_glowTextureSizeCombo->addItem("1024x768", QSize(1024,768));
    m_glowTextureSizeCombo->addItem("800x600", QSize(800,600));
    m_glowTextureSizeCombo->addItem("640x320", QSize(640,320));
    m_glowTextureSizeCombo->addItem("320x210", QSize(320,210));

    m_renderTextureSizeCombo->addItem("Window Size", QSize());
    m_renderTextureSizeCombo->addItem("1024x768", QSize(1024,768));
    m_renderTextureSizeCombo->addItem("800x600", QSize(800,600));
    m_renderTextureSizeCombo->addItem("640x320", QSize(640,320));
    m_renderTextureSizeCombo->addItem("320x210", QSize(320,210));

    for (int i=5; i<=50; i+=5) {
        m_blurSamplesCombo->addItem(QString::number(i), i);
    }
    m_blurSamplesCombo->setCurrentIndex(1);

    QWidget *w = new QWidget();
    setWidget(w);
    w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

    QFormLayout *l = new QFormLayout(w);
    l->addRow("Render Resolution", m_renderTextureSizeCombo);
    l->addRow("Render Glow", m_renderGlowCheckBox);
    l->addRow("Glow Resolution", m_glowTextureSizeCombo);
    l->addRow("Blur Samples", m_blurSamplesCombo);
    l->addRow("Scene Render Time", m_sceneRenderTimeLabel);

    connect (m_renderGlowCheckBox, SIGNAL(toggled(bool)), this, SLOT(onRenderGlowChanged(bool)));
    connect (m_glowTextureSizeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onGlowSizeChanged(int)));
    connect(m_blurSamplesCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onBlurSamplesChanged(int)));
    connect(m_renderTextureSizeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onRenderSizeChanged(int)));
}

void RenderWidget::onSceneRenderTime(double time)
{
    m_sceneRenderTimeLabel->setText(QString::number(time, 'f', 1) + " msec");
}

void RenderWidget::onGlowSizeChanged(int index)
{
    QSize size = m_glowTextureSizeCombo->itemData(index, Qt::UserRole).toSize();
    emit setPipelineParameter("glowsize", QVariant(size));
}

void RenderWidget::onRenderGlowChanged(bool render)
{
    emit setPipelineParameter("renderglow", QVariant(render));
}

void RenderWidget::onBlurSamplesChanged(int index)
{
    int samples = m_blurSamplesCombo->itemData(index, Qt::UserRole).toInt();
    emit setPipelineParameter("blursamples", QVariant(samples));
}

void RenderWidget::onRenderSizeChanged(int index)
{
    QSize size = m_renderTextureSizeCombo->itemData(index, Qt::UserRole).toSize();
    emit setPipelineParameter("rendersize", QVariant(size));
}

